package com.gzsxy.utils;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;

/**
 * @author xiaolong
 * @version 1.0
 * @description: httpclient连接池
 * @date 2021/10/8 9:42
 */
public class HttpClientUtil {

    private final CloseableHttpClient httpClient;


    /**
     * @param
     * @return
     * @description: 创建连接池
     */
    public HttpClientUtil() {
        PoolingHttpClientConnectionManager poolConnManager = new PoolingHttpClientConnectionManager();
        //最大连接数
        poolConnManager.setMaxTotal(20);
        //每个路由基础的连接
        poolConnManager.setDefaultMaxPerRoute(10);
        this.httpClient = HttpClients.custom().setConnectionManager(poolConnManager).setRetryHandler(httpRequestRetryHandler).build();
    }


    public CloseableHttpClient get() {
        return httpClient;
    }

    /**
     * @param
     * @return
     * @description: 自定义最大连接数与路由基础的连接
     */
    public HttpClientUtil(int maxtotal, int maxPerRoute) {
        PoolingHttpClientConnectionManager poolConnManager = new PoolingHttpClientConnectionManager();
        poolConnManager.setMaxTotal(maxtotal);
        poolConnManager.setDefaultMaxPerRoute(maxPerRoute);
        this.httpClient = HttpClients.custom().setConnectionManager(poolConnManager).setRetryHandler(httpRequestRetryHandler).build();
    }


    /**
     * @description: 请求重试处理
     * @param
     * @return
     */
    private final static HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext httpContext) {
            //如果已经重试5次，就放弃
            if (executionCount >= 5) {
                return false;
            }
            //如果服务器丢掉了连接，那么就重试
            if (exception instanceof NoHttpResponseException) {
                return true;
            }
            //超时
            if (exception instanceof InterruptedIOException) {
                return false;
            }
            //目标服务器不可达
            if (exception instanceof UnknownHostException) {
                return false;
            }
            //连接被拒绝
            if (exception instanceof ConnectTimeoutException) {
                return false;
            }
            HttpClientContext clientContext = HttpClientContext.adapt(httpContext);
            HttpRequest request = clientContext.getRequest();

            //如果请求是幂等的，就再次尝试
            if (!(request instanceof HttpEntityEnclosingRequest)) {
                return true;
            }
            return false;
        }
    };


    //测试
    public static void main(String[] args) {
        CloseableHttpClient httpClient = new HttpClientUtil().get();
    }
}
